package jwt.server;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class MyWebSecurityConfigurer extends WebSecurityConfigurerAdapter {


    //如果认证服务器要支持 password 模式的认证方式，需要我们提供一个AuthenticationManager对象给认证服务器，用于对 username 和 password 的判断。
    //这里直接在 Spring 容器中注入AuthenticationManager
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        PasswordEncoder noop= NoOpPasswordEncoder.getInstance();//不加密
        auth.inMemoryAuthentication()
                .withUser("guest").password("123456").roles("Guest")
                .and()
                .withUser("admin").password("123456").roles("Admin")
                .and()
                .passwordEncoder(noop);
    }


}
